﻿/*************************************
Function：压缩批处理窗体
 Version：1.0
    Date：2012-04-28
  Author：rjdong
   Email：ooxxab@gmail.com
 WebSite：http://emake.info
*************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

using Wind.Compressor.Business;
using Wind.Compressor.Entity;

namespace Wind.Compressor
{
    public partial class BatCompress2 : Form
    {
        string[] allPaths = new string[0];
        string[] allFiles = new string[0];
        public BatCompress2()
        {
            InitializeComponent();
        }

        private void bt_SourcePath_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.txt_SourcePath.Text))
                    this.folderBrowserDialog1.SelectedPath = this.txt_SourcePath.Text;
                if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.txt_SourcePath.Text = this.folderBrowserDialog1.SelectedPath;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void bt_DestPath_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.txt_DestPath.Text = this.folderBrowserDialog1.SelectedPath;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void bt_Set_Click(object sender, EventArgs e)
        {
            FormSetting form2 = new FormSetting();
            form2.ShowDialog();
        }

        private void bt_Compress_Click(object sender, EventArgs e)
        {
            try
            {
                int htmlCount = 0;
                int aspxCount = 0;
                int jsCount = 0;
                int cssCount = 0;
                int copyCount = 0;
                int errorCount = 0;
                int total = 0;
                int totalCount = 0;

                bool isIncludeJs = this.chb_Js.Checked;
                bool isIncludeCss = this.chb_Css.Checked;
                bool isIncludeHtml = this.chb_Html.Checked;
                bool isIncludeSubDir = this.chb_IsIncludeSubDir.Checked;
                bool isCopyOtherFile = this.chb_IsCopyOtherFile.Checked;

                if (string.IsNullOrEmpty(this.txt_SourcePath.Text))
                {
                    MessageBox.Show("请选择源文件目录");
                    return;
                }
                if (string.IsNullOrEmpty(this.txt_DestPath.Text))
                {
                    MessageBox.Show("请选择目标文件目录");
                    return;
                }
                SettingInfo setting = Utils.LoadSetting();

                SearchOption searchOption = SearchOption.TopDirectoryOnly;
                string fileRootPath = this.txt_SourcePath.Text;
                string destPath = this.txt_DestPath.Text;

                if (isIncludeSubDir)
                    searchOption = SearchOption.AllDirectories;

                string searchPattern = "*";

                allFiles = Directory.GetFiles(fileRootPath, searchPattern, searchOption);

                string path = string.Empty;
                string fullName = string.Empty;
                string ext = string.Empty;
                string content = string.Empty;
                string fileMidPath = string.Empty;

                setting.CssIsAutoSave = true;
                setting.JsIsAutoSave = true;

                total = allFiles.Length;
                string oper;
                if (allFiles.Length > 0)
                {
                    var n = 1;
                    this.toolStripProgressBar1.Maximum = allFiles.Length;
                    this.toolStripProgressBar1.Visible = true;
                    foreach (string file in allFiles)
                    {
                        path = Path.GetDirectoryName(file);
                        //包含子目录时，保存子目录的路径
                        fileMidPath = path.Replace(fileRootPath, "");

                        fullName = Path.GetFileName(file);
                        //name = Path.GetFileNameWithoutExtension(file);
                        ext = Path.GetExtension(file).ToLower();

                        this.toolStripStatusLabel1.Text = file;
                        this.toolStripProgressBar1.Value = n;
                        n++;

                        oper = "压缩";
                        if (ext == ".css" || ext == ".js")
                        {
                            if (ext == ".css")
                                cssCount++;
                            else if (ext == ".js")
                                jsCount++;
                            content = YuiBusiness.Instance.DoCompressBat(fileRootPath, fileMidPath, fullName, setting, destPath);
                        }
                        else if (ext == ".html" || ext == ".htm" || ext == ".aspx")
                        {
                            if (ext == ".aspx")
                                aspxCount++;
                            else
                                htmlCount++;
                            content = HtmlCompressor.DoCompress(fileRootPath, fileMidPath, fullName, setting, destPath);
                        }
                        else if (isCopyOtherFile)
                        {
                            oper = "拷贝";
                       
                            if (Utils.CopyFile(fileRootPath, fileMidPath, fullName, destPath))
                            {
                                copyCount++;
                            }
                            else
                            { 
                                errorCount++;
                            }
                        }
                        else { 
                        //不复制，不处理的文件
                            errorCount++;
                        }
                        totalCount++;
                        this.toolStripStatusLabel1.Text = "[" + totalCount + "/" + total + "]正在" + oper + "：" + fullName;
                        Application.DoEvents();

                    }
                     //MessageBox.Show("批处理完成！");
                    this.toolStripStatusLabel1.Text = "批处理完成\\(^o^)/~ Js:" + jsCount.ToString() + 
                        ",Css:" + cssCount.ToString() + 
                        ",Html:" + htmlCount.ToString() + 
                        ",aspx:" + aspxCount.ToString()+
                        ",copied file:" + copyCount.ToString() +
                        ",skip file:" + errorCount.ToString() +
                        ",total:" + total.ToString();

                    this.toolStripProgressBar1.Visible = false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write("Wind.Compressor.FormBatCompress.bt_Compress_Click error", ex);
                MessageBox.Show(ex.Message);
            }
        }

        private void link_About_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Tools.OpenHelp();
        }
    }
}
